package com.springboot.fram.core.encryption;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
 * @author liheng
 * @ClassName HmacSHA256
 * @Description
 * @date 2020-05-08 4:03
 */
public class HmacSHA256 {
    /**
     * 加密
     *
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static String encodeHmacSha256(byte[] key, byte[] data) throws Exception {
        SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA256");
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(signingKey);
        return BytesToHexString.toString(mac.doFinal(data));

    }
}
